package com.course.controller;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import com.course.db.Board;
import com.course.db.ChooseCourse;
import com.course.db.CourseBasic;
import com.course.db.CourseTerm;
import com.course.db.Pages;
import com.course.db.Student;
import com.course.db.Teacher;
import com.course.db.User;
import com.course.safe.MD5;
import com.et.mvc.JspView;
import javax.servlet.ServletConfig;
import com.jspsmart.upload.*;


public class AdminController extends ApplicationController {
	
	private ServletConfig config;

	public JspView checkCourse() throws Exception{

		System.out.println("checkcourse");
		JspView view = new JspView("admin/courseApplyApprove.jsp");
		CourseBasic course = new CourseBasic();
		
		ResultSet courseRs = course.wait_check();
	       
	       List< Hashtable<String, String> > waitcheckList = new ArrayList< Hashtable<String, String> >();
			try{
				String value="";
				ResultSetMetaData courseRsMd = courseRs.getMetaData();
				int cols = courseRsMd.getColumnCount();
				while (courseRs.next())
				{
					Hashtable<String, String> hash = new Hashtable<String, String>();
					for (int i = 1; i <= cols; i++) {	
						String field = (String) (courseRsMd.getColumnName(i));
						int fieldtype = courseRsMd.getColumnType(i);
						if(fieldtype == java.sql.Types.VARCHAR )
							value = (String) (courseRs.getString(i));
						else if(fieldtype == java.sql.Types.DOUBLE)
							value = (String)(Double.toString(courseRs.getDouble(i)));
						else if(fieldtype == java.sql.Types.INTEGER)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						else if(fieldtype == java.sql.Types.BIGINT)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						else if(fieldtype == java.sql.Types.SMALLINT)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						else if(fieldtype == java.sql.Types.TINYINT)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						else if(fieldtype == java.sql.Types.CHAR)
							value = (String) (courseRs.getString(i));
						else if(fieldtype == java.sql.Types.DATE)
							value = (String) (courseRs.getDate(i).toString());
						
						if (value == null)
							value = "";
						hash.put(field, value);
					}
					waitcheckList.add(hash);
				}
				
				session.setAttribute("WaitcheckList", waitcheckList);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			courseRs = course.show_checked();
		       
		       List< Hashtable<String, String> > checkedList = new ArrayList< Hashtable<String, String> >();
				try{
					String value="";
					ResultSetMetaData courseRsMd = courseRs.getMetaData();
					int cols = courseRsMd.getColumnCount();
					while (courseRs.next())
					{
						Hashtable<String, String> hash = new Hashtable<String, String>();
						for (int i = 1; i <= cols; i++) {	
							String field = (String) (courseRsMd.getColumnName(i));
							int fieldtype = courseRsMd.getColumnType(i);
							if(fieldtype == java.sql.Types.VARCHAR )
								value = (String) (courseRs.getString(i));
							else if(fieldtype == java.sql.Types.DOUBLE)
								value = (String)(Double.toString(courseRs.getDouble(i)));
							else if(fieldtype == java.sql.Types.INTEGER)
								value = (String)(Integer.toString(courseRs.getInt(i)));
							else if(fieldtype == java.sql.Types.BIGINT)
								value = (String)(Integer.toString(courseRs.getInt(i)));
							else if(fieldtype == java.sql.Types.SMALLINT)
								value = (String)(Integer.toString(courseRs.getInt(i)));
							else if(fieldtype == java.sql.Types.TINYINT)
								value = (String)(Integer.toString(courseRs.getInt(i)));
							else if(fieldtype == java.sql.Types.CHAR)
								value = (String) (courseRs.getString(i));
							else if(fieldtype == java.sql.Types.DATE)
								value = (String) (courseRs.getDate(i).toString());
							
							if (value == null)
								value = "";
							hash.put(field, value);
						}
						checkedList.add(hash);
					}
					
					session.setAttribute("CheckedList", checkedList);
					
				}catch(Exception e){
					e.printStackTrace();
				}
				response.sendRedirect("../admin/courseApplyApprove.jspx");
		return null;
	}
	
	public JspView adminCheck() throws Exception{
		
		System.out.println("adminCheck");
		System.out.println("id:"+request.getParameter("courseId").trim());
		int courseId = Integer.parseInt( request.getParameter("courseId").trim());
		CourseBasic course =new CourseBasic();
		course.setId(courseId);
		course.init();
		session.setAttribute("CheckCourse", course);
		
		return null;
	}
	
public JspView detail() throws Exception{
		
		System.out.println("detail");
		System.out.println("id:"+request.getParameter("courseId").trim());
		int courseId = Integer.parseInt( request.getParameter("courseId").trim());
		CourseBasic course =new CourseBasic();
		course.setId(courseId);
		course.init();
		session.setAttribute("CourseDetail", course);
		response.sendRedirect("../teacher/courseApplyApproveDetail.jspx");
		
		return null;
	}

public JspView approveDetail() throws Exception{
	
	System.out.println("detail");
	System.out.println("id:"+request.getParameter("courseId").trim());
	int courseId = Integer.parseInt( request.getParameter("courseId").trim());
	CourseBasic course =new CourseBasic();
	course.setId(courseId);
	course.init();
	session.setAttribute("CourseDetail", course);
	//response.sendRedirect("../teacher/courseApplyApproveDetail.jspx");
	
	return null;
}

public JspView selectdetail() throws Exception{
	
	System.out.println("detail");
	System.out.println("id:"+request.getParameter("courseId").trim());
	int courseId = Integer.parseInt( request.getParameter("courseId").trim());
	CourseBasic course =new CourseBasic();
	course.setId(courseId);
	course.init();
	session.setAttribute("CourseStudentDetail", course);
	
	ChooseCourse choosecourse = new ChooseCourse();
	choosecourse.setCourse_id(courseId);
	ResultSet studentRs = choosecourse.show_course_student(courseId);
	
	List< Hashtable<String, String> > coursestudentList = new ArrayList< Hashtable<String, String> >();
	try{
		String value="";
		ResultSetMetaData courseRsMd = studentRs.getMetaData();
		int cols = courseRsMd.getColumnCount();
		while (studentRs.next())
		{
			Hashtable<String, String> hash = new Hashtable<String, String>();
			for (int i = 1; i <= cols; i++) {	
				String field = (String) (courseRsMd.getColumnName(i));
				int fieldtype = courseRsMd.getColumnType(i);
				if(fieldtype == java.sql.Types.VARCHAR )
					value = (String) (studentRs.getString(i));
				else if(fieldtype == java.sql.Types.DOUBLE)
					value = (String)(Double.toString(studentRs.getDouble(i)));
				else if(fieldtype == java.sql.Types.INTEGER)
					value = (String)(Integer.toString(studentRs.getInt(i)));
				else if(fieldtype == java.sql.Types.BIGINT)
					value = (String)(Integer.toString(studentRs.getInt(i)));
				else if(fieldtype == java.sql.Types.SMALLINT)
					value = (String)(Integer.toString(studentRs.getInt(i)));
				else if(fieldtype == java.sql.Types.TINYINT)
					value = (String)(Integer.toString(studentRs.getInt(i)));
				else if(fieldtype == java.sql.Types.CHAR)
					value = (String) (studentRs.getString(i));
				else if(fieldtype == java.sql.Types.DATE)
					value = (String) (studentRs.getDate(i).toString());
				
				if (value == null)
					value = "";
				hash.put(field, value);
			}
			coursestudentList.add(hash);
		}
		
		session.setAttribute("CourseStudentList", coursestudentList);
		
	}catch(Exception e){
		e.printStackTrace();
	}
	
	response.sendRedirect("../admin/coursechooseDetail.jspx");
	
	return null;
}

public JspView deleteChoose() throws Exception{
	System.out.println("unselect");

	int student_id = Integer.parseInt(request.getParameter("studentId"));
	int courseId = Integer.parseInt(request.getParameter("courseId"));
	
	ChooseCourse choose_course = new ChooseCourse();
	choose_course.getChoosedCourse(courseId,student_id);
	
	//can not unselect course if it has been checked
	if(choose_course.getStatus() == 1 || choose_course.getStatus() == 2 )
	{
		//prompt info.....
		return null;
	}
	choose_course.delete(choose_course.getId());
	
	//update course
	CourseBasic course = new CourseBasic();
	course.setId(courseId);
	course.init();
	course.setNum_of_students(course.getNum_of_students()-1);
	course.modify_coursebasic();
	
	String src = request.getParameter("src");
	if(src==null)
		response.sendRedirect("../Admin/selectdetail.do?courseId="+courseId);
	else
	{
		response.sendRedirect("../Admin/studentSelect.do?studentId="+student_id);
		session.setAttribute("studentId", student_id);
	}
		
	
	return null;
}

public String check() throws Exception{
		
		System.out.println("detail");
			
		String checkcomment = request.getParameter("checkComment");
		System.out.println("checkcomment:"+checkcomment);
		System.out.println("id:"+request.getParameter("courseId"));
		int courseId = Integer.parseInt( request.getParameter("courseId").trim());
		int check_status = Integer.parseInt(request.getParameter("bcheck"));
		CourseBasic course =new CourseBasic();
		course.setId(courseId);
		course.init();
		course.setCheck_status(check_status);
		course.setCheckcomment(checkcomment);
		course.modify_coursebasic();
		//response.sendRedirect("../admin/courseApplyApprove.jspx");
		
		//return "审核成功！,<a href=\"#\" onclick=\"g_close_pop();window.location.href=\"../Admin/checkCourse.do\">返回</a>";
		return "<script type=\"text/javascript\">function closebox(){ top.location.href=\"../Admin/checkCourse.do\" ;this.parent.Popup.prototype.reset_m();}</script><link rel=\"stylesheet\" href=\"../css/style.css\" />审核成功！<a href=\"javascript:closebox();\">关闭</a>";
	}
	public JspView manage() throws Exception{
		
		CourseBasic course = new CourseBasic();
		ResultSet courseRs = course.show_coursebasic();
		List< Hashtable<String, String> > adminList = new ArrayList< Hashtable<String, String> >();
		try{
			String value="";
			ResultSetMetaData courseRsMd = courseRs.getMetaData();
			int cols = courseRsMd.getColumnCount();
			while (courseRs.next())
			{
				Hashtable<String, String> hash = new Hashtable<String, String>();
				for (int i = 1; i <= cols; i++) {	
					String field = (String) (courseRsMd.getColumnName(i));
					int fieldtype = courseRsMd.getColumnType(i);
					if(fieldtype == java.sql.Types.VARCHAR )
						value = (String) (courseRs.getString(i));
					else if(fieldtype == java.sql.Types.DOUBLE)
						value = (String)(Double.toString(courseRs.getDouble(i)));
					else if(fieldtype == java.sql.Types.INTEGER)
						value = (String)(Integer.toString(courseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.BIGINT)
						value = (String)(Integer.toString(courseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.SMALLINT)
						value = (String)(Integer.toString(courseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.TINYINT)
						value = (String)(Integer.toString(courseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.CHAR)
						value = (String) (courseRs.getString(i));
					else if(fieldtype == java.sql.Types.DATE)
						value = (String) (courseRs.getDate(i).toString());
					
					if (value == null)
						value = "";
					hash.put(field, value);
				}
				adminList.add(hash);
			}
			
			session.setAttribute("AdminList", adminList);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		response.sendRedirect("../admin/courseManage.jspx");
		return null;
	}
	
	public JspView edit() throws Exception{
	
		System.out.println("edit");
		
		System.out.println("id:"+request.getParameter("courseId"));
		int courseId = Integer.parseInt( request.getParameter("courseId").trim());

		//response.sendRedirect("../Admin/manage.do");

		CourseBasic course =new CourseBasic();
		course.setId(courseId);
		course.init();
		session.setAttribute("CourseEdit", course);
		response.sendRedirect("../Admin/manage.do");
		return null;
	}
	
	public JspView delete() throws Exception{

		System.out.println("delete");
		
		System.out.println("id:"+request.getParameter("courseId"));
		int courseId = Integer.parseInt( request.getParameter("courseId").trim());
		CourseBasic course = new CourseBasic();
		course.delete(courseId);
		String type = request.getParameter("type");
		if(type!=null && type.equalsIgnoreCase("teacher"))
			response.sendRedirect("../Teacher/myCourse.do");
		else
			response.sendRedirect("../Admin/manage.do");
		return null;
	}

	public JspView delopy() throws Exception{

		System.out.println("delopy");
		
		System.out.println("id:"+request.getParameter("courseId"));
		int courseId = Integer.parseInt( request.getParameter("courseId").trim());

		String type = request.getParameter("type");
		CourseBasic course = new CourseBasic();
		course.setId(courseId);
		course.init();
		if(type == null)
			course.setCheck_status(3);
		else
			course.setCheck_status(1);
		
		course.modify_coursebasic();
		response.sendRedirect("../Admin/manage.do");
		
		return null;
	}
	
	public JspView uploadStudent() throws Exception{

		System.out.println("upload student");
		SmartUpload mySmartUpload = new SmartUpload();
		mySmartUpload.initialize(config, request, response);
		System.out.println("upload student");

		Date date = new Date();
		String studentfile= new String(), fileext=new String();
		
		try {
			mySmartUpload.upload();
			
			for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++)
			{
			    SmartFile myfile = mySmartUpload.getFiles().getFile(i);
			    String fileName = myfile.getFileName();
			    if(fileName != null && !fileName.equals(""))
			    {
			    	System.out.println(fileName);
			    	studentfile = fileName.substring(0, fileName.length()-5);
				    fileext = fileName.substring(fileName.length()-4,fileName.length());
				    studentfile += date.getTime();
				    studentfile += i;
				    studentfile += fileext;

				    myfile.saveAs("files/student/"+studentfile);
			    }
			}
		} catch (Exception e) {
			  e.printStackTrace();
		}
		
		response.sendRedirect("../Admin/manage.do");
		
		return null;
	}
	
	public JspView courseList() throws Exception{
		
		CourseBasic course = new CourseBasic();
		System.out.println("select courselist");
		JspView view = new JspView("search/courseList.jsp");
		
		ResultSet courseRs = course.course_list();
	       
	       List< Hashtable<String, String> > CourseList = new ArrayList< Hashtable<String, String> >();
			try{
				String value="";
				ResultSetMetaData courseRsMd = courseRs.getMetaData();
				int cols = courseRsMd.getColumnCount();
				while (courseRs.next())
				{
					Hashtable<String, String> hash = new Hashtable<String, String>();
					for (int i = 1; i <= cols; i++) {	
						String field = (String) (courseRsMd.getColumnName(i));
						int fieldtype = courseRsMd.getColumnType(i);
						if(fieldtype == java.sql.Types.VARCHAR )
							value = (String) (courseRs.getString(i));
						else if(fieldtype == java.sql.Types.DOUBLE)
							value = (String)(Double.toString(courseRs.getDouble(i)));
						else if(fieldtype == java.sql.Types.INTEGER)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						else if(fieldtype == java.sql.Types.BIGINT)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						else if(fieldtype == java.sql.Types.SMALLINT)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						else if(fieldtype == java.sql.Types.TINYINT)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						else if(fieldtype == java.sql.Types.CHAR)
							value = (String) (courseRs.getString(i));
						else if(fieldtype == java.sql.Types.DATE)
							value = (String) (courseRs.getDate(i).toString());
						
						if (value == null)
							value = "";
						hash.put(field, value);
					}
					CourseList.add(hash);
				}
				
				session.setAttribute("AdminCourseList", CourseList);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			Student student = new Student();
			ResultSet studentRs = student.show_grade_students("高四");
		       
		       List< Hashtable<String, String> > studentList = new ArrayList< Hashtable<String, String> >();
				try{
					String value="";
					ResultSetMetaData courseRsMd = studentRs.getMetaData();
					int cols = courseRsMd.getColumnCount();
					while (studentRs.next())
					{
						Hashtable<String, String> hash = new Hashtable<String, String>();
						for (int i = 1; i <= cols; i++) {	
							String field = (String) (courseRsMd.getColumnName(i));
							int fieldtype = courseRsMd.getColumnType(i);
							if(fieldtype == java.sql.Types.VARCHAR )
								value = (String) (studentRs.getString(i));
							else if(fieldtype == java.sql.Types.DOUBLE)
								value = (String)(Double.toString(studentRs.getDouble(i)));
							else if(fieldtype == java.sql.Types.INTEGER)
								value = (String)(Integer.toString(studentRs.getInt(i)));
							else if(fieldtype == java.sql.Types.BIGINT)
								value = (String)(Integer.toString(studentRs.getInt(i)));
							else if(fieldtype == java.sql.Types.SMALLINT)
								value = (String)(Integer.toString(studentRs.getInt(i)));
							else if(fieldtype == java.sql.Types.TINYINT)
								value = (String)(Integer.toString(studentRs.getInt(i)));
							else if(fieldtype == java.sql.Types.CHAR)
								value = (String) (studentRs.getString(i));
							else if(fieldtype == java.sql.Types.DATE)
								value = (String) (studentRs.getDate(i).toString());
							
							if (value == null)
								value = "";
							hash.put(field, value);
						}
						studentList.add(hash);
					}
					
					session.setAttribute("AdminStudentList", studentList);
					
				}catch(Exception e){
					e.printStackTrace();
				}
			
			response.sendRedirect("../admin/coursechooseManage.jspx?tab=0");
		return null;
	}
	
	public JspView select() throws Exception{
		System.out.println("admin select course");
		System.out.println("courseId:"+request.getParameter("courseId"));
		System.out.println("studentId:"+request.getParameter("studentId"));
		
		int student_id = Integer.parseInt(request.getParameter("studentId"));
		int courseId = Integer.parseInt(request.getParameter("courseId"));
		

 	   	Student student = new Student();
 	   	student.setId(student_id);
 	   	student.init();
 	   	
		//check if has checked before
		ChooseCourse choose_course = new ChooseCourse();
		
		
		if( !choose_course.getChoosedCourse(courseId,student_id) )
		{
			/*
			//check grade
			if(!choose_course.check_grade(courseId).contains(student.getGrade()))
			{
				response.getWriter().append("该课程不对"+student.getGrade()+"开放，请重新选择！");
				return null;
			}
			
			//check num of selected course
			if(choose_course.course_count(student_id)>2)
			{
				response.getWriter().append("选课失败，您选课数目已超过3门！");
				return null;
			}
			
			//check num of students
			if(!choose_course.choose_num_student(courseId))
			{
				response.getWriter().append("选课失败，选择该门课程人数超过限制！");
				return null;
			}*/
			
			//update course info
			CourseBasic course = new CourseBasic();
			course.setId(courseId);
			course.init();
			course.setNum_of_students(1+course.getNum_of_students());
			course.modify_coursebasic();
			
			//add choose_course
			choose_course.setCourse_id(courseId);
			choose_course.setStudent_id(student_id);
			choose_course.add();
			
			String src = request.getParameter("src");
			if(src == null )
			{
				//response.sendRedirect("../Admin/selectdetail.do?courseId="+courseId);
			}
			else
				response.sendRedirect("../Admin/studentSelect.do?studentId="+student_id);
		}
		else
		{
			//selected, prompt info 
			response.getWriter().append("该学生已选择该门课程，请重新选择！");
			//response.sendRedirect("../Search/courseList.do");
		}
		return null;
	}
	
	
	public JspView studentSelect() throws Exception{
		System.out.println("admin student select");
		int student_id = Integer.parseInt(request.getParameter("studentId"));
		
 	   	Student student = new Student();
		student.setId(student_id);
		student.init();
 	   	
 	   	ChooseCourse choose_course = new ChooseCourse();
 	   	
 	   	ResultSet chooseRs = choose_course.show_choose_course(student_id);
 	   	
 	   List< Hashtable<String, String> > SelectList = new ArrayList< Hashtable<String, String> >();
		try{
			String value="";
			ResultSetMetaData courseRsMd = chooseRs.getMetaData();
			int cols = courseRsMd.getColumnCount();
			while (chooseRs.next())
			{
				Hashtable<String, String> hash = new Hashtable<String, String>();
				for (int i = 1; i <= cols; i++) {	
					String field = (String) (courseRsMd.getColumnName(i));
					int fieldtype = courseRsMd.getColumnType(i);
					if(fieldtype == java.sql.Types.VARCHAR )
						value = (String) (chooseRs.getString(i));
					else if(fieldtype == java.sql.Types.DOUBLE)
						value = (String)(Double.toString(chooseRs.getDouble(i)));
					else if(fieldtype == java.sql.Types.INTEGER)
						value = (String)(Integer.toString(chooseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.BIGINT)
						value = (String)(Integer.toString(chooseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.SMALLINT)
						value = (String)(Integer.toString(chooseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.TINYINT)
						value = (String)(Integer.toString(chooseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.CHAR)
						value = (String) (chooseRs.getString(i));
					else if(fieldtype == java.sql.Types.DATE)
						value = (String) (chooseRs.getDate(i).toString());
					
					if (value == null)
						value = "";
					hash.put(field, value);
				}
				SelectList.add(hash);
			}
			
			session.setAttribute("StudentSelectList", SelectList);
			session.setAttribute("studentId", request.getParameter("studentId"));
			session.setAttribute("studentname", student.getName());
			session.setAttribute("studentno", student.getNo());
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		response.sendRedirect("../admin/studentCourse.jspx");
		
		return null;
	}
	
	public JspView addBoard() throws Exception{
		System.out.println("add board");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		
		Board board = new Board();
		board.setTitle(title);
		board.setContent(content);
		
		board.add();
		
		response.getWriter().append("success");
		
		response.sendRedirect("../admin/systemManage.jspx?tab=0");
		return null;
	}
	
	public JspView userList() throws Exception{
		System.out.println("user list");
		
		String username = request.getParameter("username");
		String usertype = request.getParameter("usertype");
		session.setAttribute("type", usertype);
		
		System.out.println("username:"+username+" usertype:");
		
		User user = new User();
		List< Hashtable<String, String> > UserListTemp = new ArrayList< Hashtable<String, String> >();
		List< Hashtable<String, String> > UserList = new ArrayList< Hashtable<String, String> >();
		
		if(username != null && !username.trim().equals(""))
		{
			ResultSet userRs = user.show_user(username);;
			try{
				String value="";
				ResultSetMetaData courseRsMd = userRs.getMetaData();
				int cols = courseRsMd.getColumnCount();
				while (userRs.next())
				{
					Hashtable<String, String> hash = new Hashtable<String, String>();
					for (int i = 1; i <= cols; i++) {	
						String field = (String) (courseRsMd.getColumnName(i));
						int fieldtype = courseRsMd.getColumnType(i);
						if(fieldtype == java.sql.Types.VARCHAR )
							value = (String) (userRs.getString(i));
						else if(fieldtype == java.sql.Types.DOUBLE)
							value = (String)(Double.toString(userRs.getDouble(i)));
						else if(fieldtype == java.sql.Types.INTEGER)
							value = (String)(Integer.toString(userRs.getInt(i)));
						else if(fieldtype == java.sql.Types.BIGINT)
							value = (String)(Integer.toString(userRs.getInt(i)));
						else if(fieldtype == java.sql.Types.SMALLINT)
							value = (String)(Integer.toString(userRs.getInt(i)));
						else if(fieldtype == java.sql.Types.TINYINT)
							value = (String)(Integer.toString(userRs.getInt(i)));
						else if(fieldtype == java.sql.Types.CHAR)
							value = (String) (userRs.getString(i));
						else if(fieldtype == java.sql.Types.DATE)
							value = (String) (userRs.getDate(i).toString());
							
						if (value == null)
							value = "";
						hash.put(field, value);
					}
					UserList.add(hash);
				}
					
			}catch(Exception e){
				e.printStackTrace();
			}
			session.removeAttribute("PageFooter");
		}
		else{
			
			ResultSet adminRs = null;
			ResultSet teacherRs = null;
			ResultSet studentRs = null;
			Pages page = new Pages();
		
			if(usertype == null || usertype.equals("全部") || usertype.equals("admin"))
			{
				adminRs = user.show_admins();
				try{
					String value="";
					ResultSetMetaData courseRsMd = adminRs.getMetaData();
					int cols = courseRsMd.getColumnCount();
					while (adminRs.next())
					{
						Hashtable<String, String> hash = new Hashtable<String, String>();
						for (int i = 1; i <= cols; i++) {	
							String field = (String) (courseRsMd.getColumnName(i));
							int fieldtype = courseRsMd.getColumnType(i);
							if(fieldtype == java.sql.Types.VARCHAR )
								value = (String) (adminRs.getString(i));
							else if(fieldtype == java.sql.Types.DOUBLE)
								value = (String)(Double.toString(adminRs.getDouble(i)));
							else if(fieldtype == java.sql.Types.INTEGER)
								value = (String)(Integer.toString(adminRs.getInt(i)));
							else if(fieldtype == java.sql.Types.BIGINT)
								value = (String)(Integer.toString(adminRs.getInt(i)));
							else if(fieldtype == java.sql.Types.SMALLINT)
								value = (String)(Integer.toString(adminRs.getInt(i)));
							else if(fieldtype == java.sql.Types.TINYINT)
								value = (String)(Integer.toString(adminRs.getInt(i)));
							else if(fieldtype == java.sql.Types.CHAR)
								value = (String) (adminRs.getString(i));
							else if(fieldtype == java.sql.Types.DATE)
								value = (String) (adminRs.getDate(i).toString());
								
							if (value == null)
								value = "";
							hash.put(field, value);
						}
						UserListTemp.add(hash);
					}
						
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		
			if(usertype == null || usertype.equals("全部") || usertype.equals("teacher"))
			{
				teacherRs = user.show_teachers();
				try{
					String value="";
					ResultSetMetaData courseRsMd = teacherRs.getMetaData();
					int cols = courseRsMd.getColumnCount();
					while (teacherRs.next())
					{
						Hashtable<String, String> hash = new Hashtable<String, String>();
						for (int i = 1; i <= cols; i++) {	
							String field = (String) (courseRsMd.getColumnName(i));
							int fieldtype = courseRsMd.getColumnType(i);
							if(fieldtype == java.sql.Types.VARCHAR )
								value = (String) (teacherRs.getString(i));
							else if(fieldtype == java.sql.Types.DOUBLE)
								value = (String)(Double.toString(teacherRs.getDouble(i)));
							else if(fieldtype == java.sql.Types.INTEGER)
								value = (String)(Integer.toString(teacherRs.getInt(i)));
							else if(fieldtype == java.sql.Types.BIGINT)
								value = (String)(Integer.toString(teacherRs.getInt(i)));
							else if(fieldtype == java.sql.Types.SMALLINT)
								value = (String)(Integer.toString(teacherRs.getInt(i)));
							else if(fieldtype == java.sql.Types.TINYINT)
								value = (String)(Integer.toString(teacherRs.getInt(i)));
							else if(fieldtype == java.sql.Types.CHAR)
								value = (String) (teacherRs.getString(i));
							else if(fieldtype == java.sql.Types.DATE)
								value = (String) (teacherRs.getDate(i).toString());
								
							if (value == null)
								value = "";
							hash.put(field, value);
						}
						UserListTemp.add(hash);
					}
						
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		
			if(usertype == null || usertype.equals("全部") || usertype.equals("student"))
			{
				studentRs = user.show_students();
				try{
					String value="";
					ResultSetMetaData courseRsMd = studentRs.getMetaData();
					int cols = courseRsMd.getColumnCount();
					while (studentRs.next())
					{
						Hashtable<String, String> hash = new Hashtable<String, String>();
						for (int i = 1; i <= cols; i++) {	
							String field = (String) (courseRsMd.getColumnName(i));
							int fieldtype = courseRsMd.getColumnType(i);
							if(fieldtype == java.sql.Types.VARCHAR )
								value = (String) (studentRs.getString(i));
							else if(fieldtype == java.sql.Types.DOUBLE)
								value = (String)(Double.toString(studentRs.getDouble(i)));
							else if(fieldtype == java.sql.Types.INTEGER)
								value = (String)(Integer.toString(studentRs.getInt(i)));
							else if(fieldtype == java.sql.Types.BIGINT)
								value = (String)(Integer.toString(studentRs.getInt(i)));
							else if(fieldtype == java.sql.Types.SMALLINT)
								value = (String)(Integer.toString(studentRs.getInt(i)));
							else if(fieldtype == java.sql.Types.TINYINT)
								value = (String)(Integer.toString(studentRs.getInt(i)));
							else if(fieldtype == java.sql.Types.CHAR)
								value = (String) (studentRs.getString(i));
							else if(fieldtype == java.sql.Types.DATE)
								value = (String) (studentRs.getDate(i).toString());
								
							if (value == null)
								value = "";
							hash.put(field, value);
						}
						UserListTemp.add(hash);
					}
						
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		
			String parameters = "tab=1";
			page.setIntPageSize(20);
			page.InitPage(parameters,UserListTemp.size(), request);
			String footer = page.PageFooter(); 
			session.setAttribute("PageFooter", footer);
			
			int intPageCount = page.getIntPageCount();
			int intPage = page.getIntPage();
			int intPageSize = page.getIntPageSize();
			
			Iterator<Hashtable<String, String>> user_it = UserListTemp.iterator();
			if (intPageCount>0)  
	        {  
				System.out.println("intPageCount:"+intPageCount);
				System.out.println("intPageSize:"+intPageSize);
				System.out.println("intPage:"+intPage);
				
	           int nowpage=0; 
		    	while(user_it.hasNext())
				{
		    		Hashtable<String, String> userlist_hash  = user_it.next();
		    		nowpage++;
					if (nowpage>((intPage-1)*intPageSize) &&(nowpage<=intPage*intPageSize))  
		            {
						//System.out.println("nowpage:"+nowpage);
						Hashtable<String, String> hash = new Hashtable<String, String>();
						String username1 = userlist_hash.get("username");
						String name = userlist_hash.get("name");
						String type = userlist_hash.get("type");
						String grade = userlist_hash.get("grade");
						String introduction = userlist_hash.get("introduction");
						String user_id = userlist_hash.get("user_id");
						hash.put("username", username1);
						if(name!=null)
							hash.put("name", name);
						hash.put("type", type);
						if(grade!=null)
							hash.put("grade", grade);
						if(introduction!=null)
							hash.put("introduction", introduction);
						hash.put("user_id", user_id);
						UserList.add(hash);
		            }
				}
	        }
		}
		
		session.setAttribute("UserList", UserList);
		response.sendRedirect("../admin/systemManage.jspx?tab=1");
		
		return null;
	}
	
	public String addUser() throws Exception{
		System.out.println("add user");
		String username = request.getParameter("username");
		String usertype = request.getParameter("usertype");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		User user = new User();
		
		if(!user.userexist(username))
		{
			user.setName(username);
			MD5 oMD5 = new MD5();
			user.setPassword(oMD5.getMD5ofStr(password));
			user.setType(usertype);
			user.add();
			if(usertype.equals("student"))
			{
				Student student = new Student();
				student.setName(name);
				student.setUser_id(user.lastid());
				student.add();
			}
			else if(usertype.equals("teacher"))
			{
				Teacher teacher = new Teacher();
				teacher.setName(name);
				teacher.setUser_id(user.lastid());
				teacher.add();
			}
			return "<script type=\"text/javascript\">function closebox(){ this.parent.Popup.prototype.reset_m();}</script><link rel=\"stylesheet\" href=\"/css/style.css\" />添加成功！<a href=\"javascript:closebox();\">关闭</a>";
		}
		else
		{
			return "<script type=\"text/javascript\">function closebox(){ this.parent.Popup.prototype.reset_m();}</script><link rel=\"stylesheet\" href=\"/css/style.css\" />添加失败，用户"+username+"已存在！<a href=\"javascript:closebox();\">关闭</a>";
		}
	}
	
	public String editUserPwd() throws Exception{
		System.out.println("edit user password");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		User user = new User();
		user.setName(username);
		MD5 oMD5 = new MD5();
		user.setPassword(oMD5.getMD5ofStr(password));
		
		user.modify_user_pwd();
		
		return "<script type=\"text/javascript\">function closebox(){ this.parent.Popup.prototype.reset_m();}</script><link rel=\"stylesheet\" href=\"/css/style.css\" />修改成功！<a href=\"javascript:closebox();\">关闭</a>";
	}
	
	public JspView delUser() throws Exception{
		System.out.println("edit user password");
		String userid = request.getParameter("userid");
		User user = new User();
		user.setId(Integer.parseInt(userid));
		
		user.delete();
		
		response.getWriter().append("success");
		//response.sendRedirect("../Admin/userList.do?tab=2");
		return null;
	}
	
	public JspView editUser() throws Exception{
		System.out.println("edit user");
		String userid = request.getParameter("userid");
		int user_id = Integer.parseInt(userid);
		User user = new User();
		user.setId(Integer.parseInt(userid));
		user.init();
		
		if(user.getType().equals("student"))
		{
			Student student = new Student();
			student.setUser_id(user_id);
			int studentid = student.student_id();
			student.setId(studentid);
			student.init();
			session.setAttribute("EditStudent", student);
			response.getWriter().append("student");
		}
		else if(user.getType().equals("teacher"))
		{
			Teacher teacher = new Teacher();
			teacher.setUser_id(user_id);
			int studentid = teacher.teacher_id();
			teacher.setId(studentid);
			teacher.init();
			session.setAttribute("EditTeacher", teacher);
			response.getWriter().append("teacher");
		}
		else if(user.getType().equals("admin"))
			response.getWriter().append("admin");
		
		
		//response.getWriter().append("success");
		//response.sendRedirect("../Admin/userList.do?tab=2");
		return null;
	}
	
	public JspView changeStatus() throws Exception{
		System.out.println("change course status");
		String courseId = request.getParameter("courseId");
		int course_id = Integer.parseInt(courseId);
		String type = request.getParameter("type");
		
		//change course status to 4
		CourseBasic course = new CourseBasic();
		course.setId(course_id);
		course.init();
		if(type.equals("check"))
			course.setCheck_status(4);
		else
			course.setCheck_status(3);
		course.modify_coursebasic();
		
		//change choose_course status
		ChooseCourse choosecourse = new ChooseCourse();
		
		if(type.equals("check"))
			choosecourse.check_choosecourse(course_id,1);
		else
			choosecourse.check_choosecourse(course_id,0);
		
		
		response.sendRedirect("../Admin/courseList.do");
		
		return null;
	}
	
	public String setTerm() throws Exception{
		
		String year = request.getParameter("year");
		String term = request.getParameter("term");
		
		CourseTerm courseterm = new CourseTerm();
		courseterm.setTerm(term);
		courseterm.setYear(year);
		courseterm.add();
		
		//show term
	    int term_id = courseterm.latest_termid();
	    courseterm.setId(term_id);
	    courseterm.init();
	    request.getSession().setAttribute("CourseTerm", courseterm);
		
		return "<script type=\"text/javascript\">function closebox(){top.location.href=\"../admin/systemManage.jspx?tab=0\" ; this.parent.Popup.prototype.reset_mm();}</script><link rel=\"stylesheet\" href=\"/css/style.css\" />设置成功！<a href=\"javascript:closebox();\">关闭</a>";	
	}
	
	public String grades() throws Exception{
		
		Student student = new Student();
		
		ResultSet studentRs = student.show_grades();
		List< Hashtable<String, String> > gradeList = new ArrayList< Hashtable<String, String> >();
		try{
			String value="";
			ResultSetMetaData courseRsMd = studentRs.getMetaData();
			int cols = courseRsMd.getColumnCount();
			while (studentRs.next())
			{
				Hashtable<String, String> hash = new Hashtable<String, String>();
				for (int i = 1; i <= cols; i++) {	
					String field = (String) (courseRsMd.getColumnName(i));
					int fieldtype = courseRsMd.getColumnType(i);
					if(fieldtype == java.sql.Types.VARCHAR )
						value = (String) (studentRs.getString(i));
					else if(fieldtype == java.sql.Types.DOUBLE)
						value = (String)(Double.toString(studentRs.getDouble(i)));
					else if(fieldtype == java.sql.Types.INTEGER)
						value = (String)(Integer.toString(studentRs.getInt(i)));
					else if(fieldtype == java.sql.Types.BIGINT)
						value = (String)(Integer.toString(studentRs.getInt(i)));
					else if(fieldtype == java.sql.Types.SMALLINT)
						value = (String)(Integer.toString(studentRs.getInt(i)));
					else if(fieldtype == java.sql.Types.TINYINT)
						value = (String)(Integer.toString(studentRs.getInt(i)));
					else if(fieldtype == java.sql.Types.CHAR)
						value = (String) (studentRs.getString(i));
					else if(fieldtype == java.sql.Types.DATE)
						value = (String) (studentRs.getDate(i).toString());
						
					if (value == null)
						value = "";
					hash.put(field, value);
				}
				gradeList.add(hash);
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		session.setAttribute("GradesList", gradeList);
		return null;
	}
	
public String Courses() throws Exception{
		
		CourseBasic course = new CourseBasic();
		
		ResultSet courseRs = course.course_list();
		List< Hashtable<String, String> > gradeList = new ArrayList< Hashtable<String, String> >();
		try{
			String value="";
			ResultSetMetaData courseRsMd = courseRs.getMetaData();
			int cols = courseRsMd.getColumnCount();
			while (courseRs.next())
			{
				Hashtable<String, String> hash = new Hashtable<String, String>();
				for (int i = 1; i <= cols; i++) {	
					String field = (String) (courseRsMd.getColumnName(i));
					int fieldtype = courseRsMd.getColumnType(i);
					if(fieldtype == java.sql.Types.VARCHAR )
						value = (String) (courseRs.getString(i));
					else if(fieldtype == java.sql.Types.DOUBLE)
						value = (String)(Double.toString(courseRs.getDouble(i)));
					else if(fieldtype == java.sql.Types.INTEGER)
						value = (String)(Integer.toString(courseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.BIGINT)
						value = (String)(Integer.toString(courseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.SMALLINT)
						value = (String)(Integer.toString(courseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.TINYINT)
						value = (String)(Integer.toString(courseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.CHAR)
						value = (String) (courseRs.getString(i));
					else if(fieldtype == java.sql.Types.DATE)
						value = (String) (courseRs.getDate(i).toString());
						
					if (value == null)
						value = "";
					hash.put(field, value);
				}
				gradeList.add(hash);
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		session.setAttribute("RCourseList", gradeList);
		return null;
	}

public String idlist() throws Exception{
	
	System.out.println("idlist");
	//System.out.println("id:"+request.getParameter("courseId").trim());
	//int courseId = Integer.parseInt( request.getParameter("courseId").trim());
	//CourseBasic course =new CourseBasic();
	//course.setId(courseId);
	//course.init();
	//String ids[] = request.getParameterValues("idlist");
	String idlist = request.getParameter("idlist");
	idlist=idlist.substring(0, idlist.length()-1);
	//String ids[] = idlist.split("\\,");
	session.setAttribute("IDList", idlist);
	System.out.println("length:"+idlist);
	
	return null;
}

public String editselectCourse() throws Exception{
	
	String idlist = (String)session.getAttribute("IDList");
	String ids[] = idlist.split("\\,");
	String coursespan = request.getParameter("course_span");
	String credit = request.getParameter("credit");
	String students_max = request.getParameter("students_max");
	String students_min = request.getParameter("students_min");
	String [] grades=request.getParameterValues("grade");
	String time = request.getParameter("time");
	
	CourseBasic course = new CourseBasic();
	String course_grade = "";
	for(int j=0; j<ids.length; j++)
	{
		int courseId = Integer.parseInt(ids[j]);
		course.setId(courseId);
		course.init();
		if(grades!=null && grades[0] != null && !grades[0].equalsIgnoreCase(""))
	    {
		       for(int i=0; i<grades.length; i++)
		       {
		    	   course_grade += grades[i];
		    	   course_grade += " ";
		       }
		       course.setGrade(course_grade);
	    }
		if(coursespan!=null && !coursespan.trim().equals(""))
			course.setCourse_span(Integer.parseInt(coursespan));
		if(credit!=null && !credit.trim().equals(""))
			course.setCredit(Double.parseDouble(credit));
		if(students_max!=null && !students_max.trim().equals(""))
			course.setStudents_high(Integer.parseInt(students_max));
		if(students_min!=null && !students_min.trim().equals(""))
			course.setStudents_low(Integer.parseInt(students_min));
		if(time!=null && !time.trim().equals(""))
			course.setTeach_time(time);
		course.modify_coursebasic();
	}
	//return null;
	return "<script type=\"text/javascript\">function closebox(){top.location.href=\"../Admin/manage.do\" ; this.parent.Popup.prototype.reset_mm();}</script><link rel=\"stylesheet\" href=\"/css/style.css\" />批量编辑成功！<a href=\"javascript:closebox();\">关闭</a>";	
}

}
